<%@ page import="java.sql.Connection, 
javax.sql.DataSource, 
javax.naming.Context, 
javax.naming.InitialContext, 
java.sql.PreparedStatement, 
java.sql.ResultSet, 
java.sql.SQLException"%> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<html> 
	<body> 
		<% 
		PreparedStatement stmt = null; 
		ResultSet rs = null; 
		Connection conn = null; 

		try { 
				Context initContext = new InitialContext(); 
				Context envContext = (Context)initContext.lookup("java:/comp/env"); 
				DataSource ds = (DataSource)envContext.lookup("jdbc/myAppDB"); 
				conn = ds.getConnection(); 

				stmt = conn.prepareStatement("select id, foo, bar from testdata"); 
				rs = stmt.executeQuery(); 

				String id = null; 
				String foo = null; 
				String bar = null; 

				while (rs.next()) { 
				id = rs.getString("id"); 
				foo = rs.getString("foo"); 
				bar = rs.getString("bar"); 

				%> 
				<BR><%=id%><BR><%=foo%><BR><%=bar%><BR> 

				<% 
				} 

				rs.close(); 
				rs = null; 
				stmt.close(); 
				stmt = null; 
				conn.close(); 
				conn = null; 
		} catch (SQLException e) { 
				e.printStackTrace(); 
		} finally { 
				// Always make sure result sets and statements are closed, 
				// and the connection is returned to the pool 
				if (rs != null) { 
					try { rs.close(); } catch (SQLException e) { ; } 
					rs = null; 
				} 
				if (stmt != null) { 
					try { stmt.close(); } catch (SQLException e) { ; } 
					stmt = null; 
				} 
				if (conn != null) { 
					try { conn.close(); } catch (SQLException e) { ; } 
					conn = null; 
				} 
		} 

	%> 

	</body> 
</html> 